/* Monom.cpp  file
   ---------------
   Implementation of the Monom class methods

 */

#include <iostream>
#include "Monom.h"

using namespace std;

// cout << operator
ostream& operator<<(ostream &out, const Monom &m)
{
    /* Flags are used in order to decide what
       exactly will be printed on the screen
     */

    char printInt = 0;    /* 0 = doesn't have fraction part
                            1 = does    have fraction part
                          */

    char printC = 1;      /* 0 = don't print C coefficient
                            1 =       print C coefficient
                          */

    char printX = 2;      /* 0 = don't print X
                            1 =       print X
                            2 = print X with ^ - power sign
                          */

    // flag - determine whether num has fraction part
    if ((m.C - ((int) m.C)) == 0)
        printInt = 1;
    
    // X - should be printed or not ?
    if (m.C == 0)
        printX = 0;
    else {
        switch (m.m) {
            case 0:
                printX = 0;
                break;

            case 1:
                printX = 1;
                if (m.C == 1.0)
                    printC = 0;
                break;

            default:            // unnecessary
                if (m.C == 1.0)
                    printC = 0;
                printX = 2;
        }
    }

    if (printC == 1)
    {
        if (printInt == 1)
        {
            if (m.C == -1.0)
            {
                if (printX > 0)
                    out << "-";
                else
                    out << "-1";
            } else
                out << ((int) m.C);
        }
        else
            out << m.C;
    }

    if (printX == 1)
        out << "x";
    else if (printX == 2)
        out << "x^" << m.m;

    return out;
}
